package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.ManageTxnMapper;
import cc.rengu.oltp.service.common.entity.ManageTxn;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 管理类交易流水表操作接口实现类
 * Created by 王成 on 2018/4/18.
 */
public class ManageTxnMapperImpl implements ManageTxnMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public ManageTxnMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertManageTxn(ManageTxn manageTxn) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertManageTxn");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, manageTxn, null);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("登记管理类交易流水失败,instId:<{}>,sysSeqNum:<{}>,txnDate:<{}>,txnTime:<{}>,txnNum:<{}>,msgSrcId:<{}>,transChannelId:<{}>,frontSeqNum:<{}>,iReturnCode:<{}>",
                    manageTxn.getInstId(), manageTxn.getSysSeqNum(), manageTxn.getTxnDate(), manageTxn.getTxnTime(), manageTxn.getTxnNum(), manageTxn.getMsgSrcId(), manageTxn.getTransChannelId(), manageTxn.getFrontSeqNum(), iReturnCode);
            return iReturnCode;
        }
        return Database.DBS_SUCCESS;
    }

    @Override
    public int updateManageTxnByPrimaryKey(ManageTxn manageTxn) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateManageTxnByPrimaryKey");
        manageTxn.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, manageTxn, null);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateManageTxnHisByPrimaryKey");
            iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, manageTxn, null);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("更新管理类交易流水失败,instId:<{}>,sysSeqNum:<{}>,txnDate:<{}>,txnTime:<{}>,txnNum:<{}>,msgSrcId:<{}>,transChannelId:<{}>,frontSeqNum:<{}>,iReturnCode:<{}>",
                        manageTxn.getInstId(), manageTxn.getSysSeqNum(), manageTxn.getTxnDate(), manageTxn.getTxnTime(), manageTxn.getTxnNum(), manageTxn.getMsgSrcId(), manageTxn.getTransChannelId(), manageTxn.getFrontSeqNum(), iReturnCode);
                return iReturnCode;
            }
        }
        return Database.DBS_SUCCESS;
    }

    @Override
    public ManageTxn selectManageTxnByFrontSeqNum(String instId, String transDateTime, String frontSeqNum, String msgSrcId, String transChannelId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectManageTxnByFrontSeqNum");
        ManageTxn manageTxn = new ManageTxn();
        manageTxn.setInstId(instId);
        manageTxn.setTransDateTime(transDateTime);
        manageTxn.setFrontSeqNum(frontSeqNum);
        manageTxn.setMsgSrcId(msgSrcId);
        manageTxn.setTransChannelId(transChannelId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, manageTxn, manageTxn);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.warn("根据前端流水号查询管理类流水失败,instId:<{}>,transDateTime:<{}>,frontSeqNum:<{}>,srcSysId:<{}>,transChannelId:<{}>,iReturnCode:<{}>", instId, transDateTime, frontSeqNum, msgSrcId, transChannelId, iReturnCode);
            return null;
        }
        return manageTxn;
    }

    @Override
    public ManageTxn selectManageTxnByPrimaryKey(String instId, String sysSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectManageTxnByPrimaryKey");
        ManageTxn manageTxn = new ManageTxn();
        manageTxn.setInstId(instId);
        manageTxn.setSysSeqNum(sysSeqNum);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, manageTxn, manageTxn);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.warn("根据系统流水号查询管理类流水失败,instId:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>", instId, sysSeqNum, iReturnCode);
            return null;
        }
        return manageTxn;
    }

    @Override
    public List<ManageTxn> selectManageTxnByMchntNo(String instId, String mchntNo, String beginDate, String endDate) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectManageTxnByMchntNo");
        ManageTxn manageTxn = new ManageTxn();
        manageTxn.setInstId(instId);
        manageTxn.setMchntNo(mchntNo);
        manageTxn.setTxnDate(beginDate);
        manageTxn.setRemark1(endDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, manageTxn, ManageTxn.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据商户号查询管理类流水失败,instId:<{}>,mchntNo:<{}>,beginDate:<{}>,endDate:<{}>", instId, mchntNo, beginDate, endDate);
            return null;
        }
        return objectList.stream().map(item -> (ManageTxn) (item)).collect(Collectors.toList());
    }

    @Override
    public List<ManageTxn> selectManageTxnByUserId(String instId, String userId, String beginDate, String endDate) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectManageTxnByUserId");
        ManageTxn manageTxn = new ManageTxn();
        manageTxn.setInstId(instId);
        manageTxn.setUserId(userId);
        manageTxn.setTxnDate(beginDate);
        manageTxn.setRemark1(endDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, manageTxn, ManageTxn.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据用户标识查询管理类流水失败,instId:<{}>,userId:<{}>,beginDate:<{}>,endDate:<{}>", instId, userId, beginDate, endDate);
            return null;
        }
        return objectList.stream().map(item -> (ManageTxn) (item)).collect(Collectors.toList());
    }
}
